SpringBoot | 您所在的位置:网站首页 › 解决Spring Boot访问resources目录下的static资源问 › SpringBoot |
1.默认的静态资源目录
/static /public /resources /META-INF/resources 动态资源目录:/templates 2.resources静态资源目录图片存放 3. 静态资源访问 3.1.通过路径访问静态资源http://localhost:8080/a.jpg http://localhost:8080/b.jpg http://localhost:8080/c.png http://localhost:8080/d.jpg 3.2.通过配置类配置路径访问本地静态资源 1.config @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //自定义路径mypic, addResourceLocations指定访问资源所在目录 registry.addResourceHandler("/mypic/**").addResourceLocations("file:C:\\Users\\Administrator\\Desktop\\images1\\"); //自定义路径webjars访问,addResourceLocations映射该路径下的资源,resourceChain资源链 // registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/").resourceChain(true); } } 2.访问结果展示路径:http://localhost:8080/mypic/huangshanpic.webp 3.3.通过配置文件配置路径访问静态资源 (1).application.yml web.pic-path=C:/Users/Administrator/Desktop/images1/ spring.mvc.static-path-pattern=/mypic/** spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/public/,classpath:/static/,file:${web.pic-path}web.pic-path:访问路径 spring.mvc.static-path-pattern:采用全部映射到mypic路径的方式 spring.web.resources.static-locations:配置允许访问的静态资源目录 (2).访问路径格式http://localhost:8080/mypic/a.jpg http://localhost:8080/mypic/b.jpg http://localhost:8080/mypic/c.png http://localhost:8080/mypic/d.jpg http://localhost:8080/mypic/web.pic-path配置本地路径下的图片名称 3.4. 通过引入打包静态资源的jar包形式访问 (1).静态资源打jar包创建一个新的web工程,只存放静态资源 1).pom.xml WWebjarsdemo 1.0 ${project.basedir}/src/main/resources ${project.build.outputDirectory}/META-INF/resources/webjars2).静态资源目录结构 3). package点击打包 4).install到本地仓库 (2).主项目中引入依赖包1).pom.xml com.openlab WWebjarsdemo 1.0 org.webjars webjars-locator-core 0.35 (3).路径访问未引入webjars-locator-core的jar包:http://localhost:8080/webjars/example/版本号/huangshan.webp 引入webjars-locator-core的jar包: http://localhost:8080/webjars/example/huangshan.webp 注意:如果主程序和引入打包的jar包静态资源下具备相同的目录结构,如:META-INF\resources\webjars\example\0.0.1\**,可能会出现路径访问失败的情况。 解决方法:clean主程序项目,重新运行。 (4).访问结果 |
CopyRight 2018-2019 实验室设备网 版权所有 |